home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / tex / ascutils.zip / SUFFIX.ASM < prev    next >
Assembly Source File  |  1988-05-24  |  6KB  |  235 lines

  1. ;                   SUFFIX
  2. ;
  3.  
  4. sfx_len        equ    050h
  5. ibuf_len    equ    0100h
  6. obuf_len    equ    0150h
  7. stk_len        equ    0100h
  8. total_buflen    =    sfx_len    + ibuf_len + obuf_len +    stk_len
  9.  
  10. codeseg        segment
  11.         assume    cs:codeseg, ds:codeseg
  12.  
  13.         org    100h
  14.  
  15. suffix        proc    far
  16. start:        jmp    short init
  17.  
  18. ; ---------------------------------
  19. msg1        db    13, 10,    'Usage is: SUFFIX <quoted string>', 13,    10, '$'
  20. msg2        db    13, 10,    'Unable    to allocate memory - aborting.'
  21.         db    13, 10,    '$'
  22. ; ---------------------------------
  23.  
  24. init:        mov    bx,offset pgm_end
  25.         add    bx,0Fh
  26.         mov    cl,4
  27.         shr    bx,cl
  28.         mov    ah,04Ah        ; allocate memory explicitly
  29.         int    021h
  30.         mov    dx,offset msg2    ; set up error message
  31.         jc    err_exit    ; quit if cf=1
  32.         xor    ax,ax            ; init buffers and stk
  33.         mov    di,offset last_line           ;  to all 0's
  34.         mov    cx,total_buflen
  35.         shr    cx,1
  36.         cld
  37.         rep    stosw
  38.         mov    sp,offset pgm_end
  39.  
  40.         mov    si,81h        ; point    si to command line parms
  41.         call    get_suffix    ; get the quoted suffix    string
  42.         mov    dx,offset msg1    ; set up other error msg
  43.         cmp    al,5        ; success code
  44.         jne    err_exit    ; unsuccessful - quit with err msg
  45.         call    get_suffix    ; This checks for extra    stuff on
  46.         mov    dx,offset msg1
  47.         cmp    al,3        ;  the command line, and quits with
  48.         jne    err_exit    ;  error msg if    found.
  49.  
  50. main:        call    get_line    ; input    a line from std    in
  51.         jc    quit        ;  quit    on carry set (eof or error)
  52.         call    set_suffix    ; suffix it
  53.         call    out_put        ; output it to std out
  54.         jc    quit        ;  quit    on error.
  55.         jmp    main        ; repeat until eof(input)
  56.  
  57.  
  58. err_exit:    mov    di,dx
  59.         mov    cx,0FFh
  60.         mov    al,024h
  61.         cld
  62.         repnz    scasb        ; this was to get len of message
  63.         dec    di
  64.         mov    cx,di
  65.         sub    cx,dx        ; some arithmetic
  66.         mov    bx,2        ; handle for std error
  67.         mov    ah,040h        ; output msg to    std error
  68.         int    021h        ;  (error msg will go to con)
  69.         mov    al,1        ; exit status =    1
  70. quit:        mov    ah,04Ch
  71.         int    021h        ; terminate process.
  72.  
  73. suffix        endp
  74.  
  75.  
  76. ; ----------------------------------------------------------------------------
  77. ;        SUBROUTINE  get_suffix
  78. ;        get the    quoted suffix into the holding area
  79. ;
  80.  
  81. get_suffix    proc    near
  82.         call    rem_spc            ; skips    spaces on cmd line
  83.         mov    di,si            ; save si in di
  84.         cmp    byte ptr [si],0Dh    ; see if we have eoln
  85.         jne    Q1            ; no, continue
  86.         mov    al,3            ;  else    return eoln code.
  87.         ret
  88.  
  89. Q1:        mov    dl,022h            ; double quote
  90.         cmp    [si],dl
  91.         je    P1            ; ok, continue
  92.         mov    dl,027h            ; single quote
  93.         cmp    [si],dl
  94.         je    P1            ; still    ok, continue
  95.         mov    al,4            ; no, return non-quote code.
  96.         ret
  97.  
  98. P1:        inc    si        ; now get quoted string    into vacant
  99.         mov    di,offset last_line    ; suffix    buffer area
  100.  
  101. Q2:        lodsb            ; get [si] into    al
  102.         cmp    al,dl        ; check    for matching quote, still in
  103.         je    Q3        ; dl.  If found, we're done.
  104.         cmp    al,0Dh        ; check    for eoln
  105.         je    P2        ; if found here, error
  106.         stosb            ; OK - store al    to [es:di]
  107.         cmp    di,offset in_buffer
  108.         je    P2        ;  too many chars, exit    with error
  109.         jmp    Q2        ; repeat until done or error.
  110. Q3:        mov    al,5        ; success.
  111.  
  112. P3:        mov    byte ptr [di],0        ; null terminate the string
  113.         mov    di,offset last_line           ; reset di
  114.         ret
  115.  
  116. P2:        mov    al,6        ; error    (non-success) code
  117.         jmp    P3
  118.  
  119. get_suffix    endp
  120.  
  121. ; --------------------------------------------------------------------------
  122. ;        SUBROUTINE  get_line
  123. ;        inputs a string    (line) from std    in
  124.  
  125. get_line    proc    near
  126.         mov    si,offset in_buffer    ; point    si to input buffer
  127.  
  128. G1:        mov    dx,si            ; point    dx to input buffer
  129.         mov    cx,1            ; set char count to 1
  130.         mov    bx,0            ; select handle    for input
  131.         mov    ah,3Fh            ; DOS input from std in
  132.         int    21h
  133.  
  134.         jc    getline_ret        ; error, code in ax
  135.         stc
  136.         mov    cx,ax            ; ax = chars read
  137.         jcxz    getline_ret        ; return if no chars read
  138.         mov    al,0            ; set exit status to 0
  139.         cmp    byte ptr [si],0Ah    ; eoln,    return
  140.         je    getline_ret
  141.         cmp    byte ptr [si],1Ah    ; check    for eof
  142.         stc
  143.         jz    getline_ret        ; eof, ret w/carry set
  144.         inc    si            ; inc si
  145.         cmp    si,offset out_buffer    ; check    for buffer overrun
  146.         jb    G1            ; OK, repeat
  147.         mov    al,1            ; not OK, exit status 1
  148.         stc                ;  ret w/carry set
  149.  
  150. getline_ret:    ret
  151.  
  152. get_line    endp
  153.  
  154. ; -------------------------------------------------------------------------
  155. ;        SUBROUTINE  set_suffix
  156. ;        suffixes the line that was input
  157.  
  158. set_suffix    proc    near
  159.         cld
  160.         mov    di,offset out_buffer    ; point    to output buffer
  161.  
  162. ; first    move the input line into the output buffer:
  163.  
  164.         mov    si,offset in_buffer    ; set si to input buffer
  165.  
  166. S1:        lodsb                ; [si] into al
  167.         stosb                ; al to    [es:di]
  168.         cmp    al,0Dh            ; check    for eoln
  169.         jne    S1            ; if yes, goto S2
  170.  
  171. ; now move the suffix into the output buffer:
  172.  
  173.         mov    si,offset last_line           ; restore suffix    ptr to si
  174.         dec    di            ; move di back 1 char
  175. S2:        lodsb                ; [si] into al
  176.         cmp    al,0            ; end of suffix    (null term.)
  177.         je    S3            ; now finish line
  178.         stosb                ; al to    [es:di]
  179.         jmp    S2            ; repeat
  180. S3:        mov    al,0Dh            ; put the eoln on output.
  181.         stosb
  182.         mov    al,0Ah
  183.         stosb
  184.         ret                ; done - return
  185. set_suffix    endp
  186.  
  187. ; -------------------------------------------------------------------------
  188. ;        SUBROUTINE  out_put
  189. ;        outputs    the string (line) to std out
  190.  
  191. out_put        proc    near
  192.         mov    si,offset out_buffer
  193.         mov    dx,si        ; put si in dx
  194.         xor    cx,cx        ; start    cx at 0
  195.  
  196. O1:        inc    cx        ; pre-increment    for char count
  197.         cmp    cx,obuf_len    ; check    for buffer overrun
  198.         stc
  199.         jge    output_ret    ; suffix + line    too long, exit
  200.         lodsb            ; [si] into al
  201.         cmp    al,0Ah        ; see if it's eoln
  202.         jne    O1        ; if not, repeat.
  203.         mov    bx,1        ; set up std out
  204.         mov    ah,040h        ; output cx characters.
  205.         int    021h
  206.  
  207. output_ret:    ret            ; carry    set if error.
  208.  
  209. out_put        endp
  210.  
  211. ; -------------------------------------------------------------------------
  212. ;        SUBROUTINE
  213. ;        skips over spaces
  214.  
  215. rem_spc        proc    near
  216. R1:
  217.         cmp    byte ptr [si],20h
  218.         jne    rem_spc_ret
  219.         inc    si
  220.         jmp    R1
  221.  
  222. rem_spc_ret:    ret
  223. rem_spc        endp
  224.  
  225. ; --------------------------------------------------------------------------
  226. ;  buffer allocation:
  227. last_line    label    word
  228.  
  229. in_buffer    =    last_line + sfx_len
  230. out_buffer    =    in_buffer + ibuf_len
  231. pgm_end        =    out_buffer + obuf_len +    stk_len
  232.  
  233. codeseg        ends
  234.         end    start
  235.